package com.example.administrator.videodemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private boolean isExit = false; //准备退出  false-没准备好；true-准备好了

    String mVideoUrl = "http://video.yidianzixun.com/video/get-url?key=user_upload/1506676120279e7dfccbcd862406e77b53bcfac41380e.mp4";

    private NiceVideoPlayer mNiceVideoPlayer;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init() {
        mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
        mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // or NiceVideoPlayer.TYPE_NATIVE
        mNiceVideoPlayer.setUp(mVideoUrl, null);

        TxVideoPlayerController controller = new TxVideoPlayerController(this);
        controller.setTitle("gaga");
        controller.setImage(R.mipmap.ic_launcher);
        mNiceVideoPlayer.setController(controller);

        webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "onReceivedError");
                webView.loadUrl("file:///android_asset/error.html");
            }
        });
        webView.loadUrl("http://www.baidu.com/");
    }
    @Override
    protected void onStop() {
        super.onStop();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd()) return;
        super.onBackPressed();
    }

    /**
     * 连续点击两次退出
     */
    private void exitBy2Click() {
        if (isExit) {   //准备好了（即连续点击了两次），直接退出
            finish();
        } else {
            isExit = true;  //准备退出
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            Timer tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        }
    }

    /**
     * 回退，连续点击两次退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            exitBy2Click();
        }
        return false;
    }
}


